package com.byxy.luntan.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byxy.luntan.dao.ForumDao;
import com.byxy.luntan.dao.TabDao;
import com.byxy.luntan.dao.TabVoDao;
import com.byxy.luntan.dao.TipDao;
import com.byxy.luntan.dao.UserDao;
import com.byxy.luntan.entity.Forum;
import com.byxy.luntan.entity.Tab;
import com.byxy.luntan.entity.Tip;
import com.byxy.luntan.entity.User;
import com.byxy.luntan.service.MainService;
import com.byxy.luntan.vo.TipVO;





@Service
@Transactional
public class MainServiceImpl  implements MainService{

	@Autowired
	private TipDao tipDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ForumDao forumDao;
	@Autowired
	private TabDao tabDao;

	@Autowired
	private TabVoDao tabVodao;
	
	
	@Override
	public List<TipVO> find() {
		// 代码拼接数据
				List<Tip> tips = tipDao.findByPage(2, 3);// 与数据库打交道,从数据库取出数据
				//PageHelper.startPage(2, 3);
				//List<Tip> tips = tipDao.find();
				List<TipVO> tipVos = new ArrayList<>();
		///// 组合数据
		for (Tip p : tips) {
			TipVO vo = new TipVO();
			BeanUtils.copyProperties(p, vo);// 将数据取出的数据赋给TipVO集合
			User u = userDao.getById(p.getUserId());
			if (u.getUserNick() != null && u.getUserNick().equals("")) {
				vo.setUserName(u.getUserNick());
			} else {
				vo.setUserName(u.getUserName());
			}
			Tab tab = tabDao.getById(p.getTabId());
			vo.setTabName(tab.getTabName());
			Forum forum = forumDao.getById(tab.getForumId());
			vo.setForumName(forum.getForumName());
			tipVos.add(vo);
		}

		return tipVos;
	}

	
	@Override
	public List<TipVO> findByVO() {
		//	SQL脚本拼装数据
		return tabVodao.find();
	}
}
